package com.atguigui.leetcode;

import java.util.Arrays;

/**
 * 870.优势洗牌
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2022/10/8 22:14
 */
public class P870AdvantageShuffle {
    public static void main(String[] args) {
        Solution solution = new P870AdvantageShuffle().new Solution();


        int[] n1 = {1, 10, 4, 11};
        Integer[] idx1 = new Integer[n1.length];
        for (int i = 0; i < n1.length; i++) {
            idx1[i] = i;
        }
        Arrays.sort(idx1, (x, y) -> n1[x] - n1[y]);

        Arrays.stream(idx1).forEach(System.out::println);

        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] advantageCount(int[] nums1, int[] nums2) {
            int n = nums1.length;
            Integer[] idx1 = new Integer[n];
            Integer[] idx2 = new Integer[n];
            for (int i = 0; i < n; i++) {
                idx1[i] = i;
                idx2[i] = i;
            }
            //使idx1中的数据顺序和nums1中的数据顺序一致
            Arrays.sort(idx1, (x, y) -> nums1[x] - nums1[y]);
            Arrays.sort(idx2, (x, y) -> nums2[x] - nums2[y]);

            int[] ans = new int[n];
            int left = 0, right = n - 1;
            for (int i = 0; i < n; ++i) {
                //将数据排序后一次比较
                if (nums1[idx1[i]] > nums2[idx2[left]]) {
                    ans[idx2[left]] = nums1[idx1[i]];
                    ++left;
                } else {
                    ans[idx2[right]] = nums1[idx1[i]];
                    --right;
                }
            }

            return ans;
        }
    }
}
